package com.example.smartcity_poverty.ui.AllService.ActivityManAge

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.model.ActivityManAge.HBanner
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.ActivityManAge.fragment.ActivityManAgeFragment
import com.example.smartcity_poverty.ui.AllService.ActivityManAge.fragment.ActivityManAgeFragment.Companion.HId
import com.google.android.material.tabs.TabLayout
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.activity_man_age.*

/**
 * 活动管理.
 */
class ManAgeActivity : AppCompatActivity() {

    private val vm by lazy {
        ViewModelProviders.of(this)[ManAgeViewModel::class.java]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_man_age)
        title = "活动管理"

        // Banner
        setBanner()

        // tabLayout
        setTabLayout()

    }

    /**
     * 活动分类
     */
    private fun setTabLayout() {
        val array = listOf<String>("商务","生活","亲子","行业","校园","学习")

        for (i in array){
            HTabLayout.addTab(HTabLayout.newTab().setText(i))
        }



        // ViewPager2Adapter
        HViewPager2.adapter = object : FragmentStateAdapter(this){
            override fun getItemCount(): Int = array.size

            override fun createFragment(position: Int): Fragment
                    = when(position){
                0 -> ActivityManAgeFragment()
                1 -> ActivityManAgeFragment()
                2 -> ActivityManAgeFragment()
                3 -> ActivityManAgeFragment()
                4 -> ActivityManAgeFragment()
                else -> ActivityManAgeFragment()
            }

            override fun getItemId(position: Int): Long {
                HId = when(position){
                    0 -> 1
                    1 -> 2
                    2 -> 3
                    3 -> 4
                    4 -> 5
                    else -> 6
                }
                return super.getItemId(position)
            }
        }



        // 监听ViewPager2滑动
        HViewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                HTabLayout.getTabAt(position)?.select()
            }
        })

        // 监听TabLayout点击事件
        HTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
               HViewPager2.currentItem = tab?.position!!
            }

        })
    }

    // 设置轮播图
    private fun setBanner() {
        vm.bannerLiveData.observe(this, Observer { resources ->
            resources.getOrNull()?.apply {
                if (this.code == "200"){
                    vm.bannerList.clear()
                    vm.bannerList.addAll(this.rows!!)

                    // 设置轮播图
                    HBanner.apply {
                        this.adapter = object : BannerImageAdapter<HBanner.RowsBean>(vm.bannerList){
                            override fun onBindView(
                                p0: BannerImageHolder?,
                                p1: HBanner.RowsBean?,
                                p2: Int,
                                p3: Int
                            ) {
                                // 网络图片加载器
                                glide(url + p1?.advImg, p0?.imageView!!)
                            }

                        }
                        this.addBannerLifecycleObserver(this@ManAgeActivity).indicator = CircleIndicator(context)
                        this.setBannerRound(25f)
                    }

                }else "加载轮播图失败！".show()
            }
        })
    }




    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}